/* 
 * @moduleName: TransFormBetweenUtil.js
 * @Desc: 范围选择器(增加验证...)
 * @Author: djkloop 
 * @Date: 2017-11-30 12:46:03 
 * @Last Modified by: djkloop
 * @Last Modified time: 2017-12-12 02:57:32
 */

 import React from 'react'
 import betweenUtil from './BetweenColWrapperUtil'
 import {InputNumber,Form,Col,DatePicker} from 'antd'
 import moment from 'moment'
 const { RangePicker } = DatePicker;
 const FormItem = Form.Item

 export default {
  transformBetween(field,type) {
    let beginFormItem;
    let endFormItem;
    let _rules = field.dateRules && type === 'single' ? field.dateRules.rules : [];
    switch (field.dataType) {
      case 'int':
        beginFormItem = (getFieldDecorator, forUpdate) => getFieldDecorator(`${field.key}Begin`, {rules:_rules,initialValue: forUpdate ? undefined : field.defaultValue})(<InputNumber size="default" min={field.min} placeholder={field.placeholderBegin || '最小值'}/>);
        endFormItem = (getFieldDecorator, forUpdate) => getFieldDecorator(`${field.key}End`, {rules:_rules,initialValue: forUpdate ? undefined : field.defaultValue})(<InputNumber size="default" min={field.min} placeholder={field.placeholderEnd || '最大值'}/>);
        return betweenUtil.betweenColWrapper(beginFormItem, endFormItem, field);
      case 'float':
        beginFormItem = (getFieldDecorator, forUpdate) => getFieldDecorator(`${field.key}Begin`, {rules:_rules,initialValue: forUpdate ? undefined : field.defaultValue})(<InputNumber step={0.01} size="default" placeholder={field.placeholderBegin || '最小值'}/>);
        endFormItem = (getFieldDecorator, forUpdate) => getFieldDecorator(`${field.key}End`, {rules:_rules,initialValue: forUpdate ? undefined : field.defaultValue})(<InputNumber step={0.01} size="default" placeholder={field.placeholderEnd || '最大值'}/>);
        return betweenUtil.betweenColWrapper(beginFormItem, endFormItem, field);
      case 'datetimeRange':
        let nowTime = moment();
        let time;
        // 这个三元写的我真是mmp
        field.defaultValue ? time = [field.defaultValue.defaultValueBegin?moment(field.defaultValue.defaultValueBegin === 'now' ? nowTime : field.defaultValue.defaultValueBegin, field.dateFormat):null, field.defaultValue.defaultValueEnd?moment(field.defaultValue.defaultValueEnd === 'now' ? nowTime : field.defaultValue.defaultValueEnd, field.dateFormat): null] : time = null
        return (getFieldDecorator, forUpdate) => (
          <div key={`${field.key}datetimeBetweenRangeDiv`}>
            <Col key={`${field.key}`} span={8}>
              <FormItem key={`${field.key}`} label={field.title} labelCol={{ span: 5 }} wrapperCol={{ span:2 }}>
                {
                  getFieldDecorator(`${field.key}`, {rules:_rules,initialValue: forUpdate ? void 0 : time})(
                   <RangePicker showTime format={field.dateFormat} placeholder={field.placeholderTip}/>)
                }
              </FormItem>
            </Col>
          </div>);
        case 'datetime':
          return (getFieldDecorator, forUpdate) => (
            <div key={`${field.key}datetimeBetweenDiv`}>
              <Col key={`${field.key}Begin`} sm={8}>
                <FormItem key={`${field.key}Begin`} label={field.title} labelCol={{ span: 10 }}
                          wrapperCol={{ span:5 }}>
                  {
                    getFieldDecorator(`${field.key}Begin`, {rules:_rules,initialValue: forUpdate ? void 0 : field.defaultValueBegin ? moment(field.defaultValueBegin) : null})(<DatePicker showTime format="YYYY-MM-DD HH:mm:ss" placeholder={field.placeholderBegin || '开始日期'}/>)}
                </FormItem>
              </Col>
              <Col key={`${field.key}End`} sm={4}>
                <FormItem key={`${field.key}End`} labelCol={{ span: 10 }} wrapperCol={{ span:14 }}>
                  {
                    getFieldDecorator(`${field.key}End`, {rules:_rules,initialValue: forUpdate ? void 0 : field.defaultValueEnd ? moment(field.defaultValueEnd) : null})(<DatePicker showTime format="YYYY-MM-DD HH:mm:ss" placeholder={field.placeholderEnd || '结束日期'}/>)}
                </FormItem>
              </Col>
            </div>
          );
      default:
        console.log('没有dataType!')
    }
    return null
  }
 }